package cn.yyh.order.service.impl;

import cn.yyh.basic.service.impl.BaseServiceImpl;
import cn.yyh.basic.utils.CodeGenerateUtils;
import cn.yyh.basic.utils.PageInfo;
import cn.yyh.order.domain.AdoptOrder;
import cn.yyh.order.domain.OrderAddress;
import cn.yyh.order.mapper.AdoptOrderMapper;
import cn.yyh.order.mapper.OrderAddressMapper;
import cn.yyh.order.query.AdoptOrderQuery;
import cn.yyh.order.service.IAdoptOrderService;
import cn.yyh.org.domain.Employee;
import cn.yyh.org.mapper.EmployeeMapper;
import cn.yyh.pet.domain.Pet;
import cn.yyh.pet.mapper.PetMapper;
import cn.yyh.user.mapper.UserAddressMapper;
import cn.yyh.user.mapper.UserMapper;
import cn.yyh.user.pojo.po.User;
import cn.yyh.user.pojo.po.UserAddress;
import cn.yyh.user.utils.JudgeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrderMapper, AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Override
    public String submit(Map<String, Object> map, Long id) {

        // 1.数据校验
        Integer serviceMethod = Integer.valueOf(map.get("service_method").toString());
        Long addressId = Long.valueOf(map.get("address_id").toString());
        Integer payMethod = Integer.valueOf(map.get("pay_method").toString());
        Long petId = Long.valueOf(map.get("pet_id").toString());

        JudgeUtil.isNull(serviceMethod,addressId,payMethod,petId);

        // 2.把宠物下架,并绑定userId
        User user = userMapper.getByLoginInfoId(id);
        Pet pet = petMapper.getById(petId);
        pet.setOffsaletime(new Date());
        pet.setState(0);
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        // 3.生成订单
        AdoptOrder adoptOrder = initAdoptOrder(user, pet);
        baseMapper.save(adoptOrder);

        // 4.生成对应的订单地址
        OrderAddress orderAddress = initOrderAddress(addressId, adoptOrder);
        orderAddressMapper.save(orderAddress);


        // @TODO 5.支付订单并支付
        // @TODO 6.超时未支付自动取消

        return null;
    }

    @Override
    public PageInfo<AdoptOrder> queryByAdmin(AdoptOrderQuery query, Long id) {
        Employee employee = employeeMapper.getByLoginInfoId(id);
        if (employee.getShopId() != null) {
            query.setShopId(employee.getShopId());
        }
        return super.pageQuery(query);
    }

    @Override
    public PageInfo<AdoptOrder> queryByUser(AdoptOrderQuery query, Long id) {
        User user = userMapper.getByLoginInfoId(id);
        query.setUserId(user.getId());
        return super.pageQuery(query);
    }

    private OrderAddress initOrderAddress(Long addressId, AdoptOrder adoptOrder) {
        UserAddress userAddress = userAddressMapper.getById(addressId);
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setCreateTime(new Date());
        orderAddress.setUpdateTime(new Date());
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder initAdoptOrder(User user, Pet pet) {
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("[摘要]对{"+ pet.getName()+"}的领养订单！");
        adoptOrder.setPrice(pet.getSaleprice().add(new BigDecimal(10)));
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        adoptOrder.setOrderSn(orderSn);
        adoptOrder.setLastPayTime(new Date(System.currentTimeMillis()+1000*60*15));
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        return adoptOrder;
    }
}
